package com.cxh.rmsq.wxapi

import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.event.EventMessage
import com.cxh.rmsq.event.LiveEventBusCode
import com.cxh.rmsq.ui.BaseActivity
import com.cxh.rmsq.utils.ToastUtils
import com.cxh.rmsq.wx.WXManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler

class WXEntryActivity : BaseActivity(), IWXAPIEventHandler {
    companion object {
        private const val RETURN_MSG_TYPE_LOGIN = 1
        private const val RETURN_MSG_TYPE_SHARE = 2
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WXManager.getInstance().wxapi.handleIntent(intent, this)
    }

    override fun onReq(baseReq: BaseReq) {
    }

    override fun onResp(resp: BaseResp) {
        when (resp.errCode) {
            BaseResp.ErrCode.ERR_AUTH_DENIED, BaseResp.ErrCode.ERR_USER_CANCEL -> {
                when (resp.type) {
                    RETURN_MSG_TYPE_SHARE -> {
                        finish()
                        ToastUtils.showToast("分享失败")
                    }

                    else -> {
                        ToastUtils.showToast("登录失败")
                        finish()
                        LiveEventBus.get(LiveEventBusCode.WECHAT_LOGIN_ERR).post(resp.type)
                    }
                }
            }

            BaseResp.ErrCode.ERR_OK -> {
                when (resp.type) {
                    RETURN_MSG_TYPE_LOGIN -> {
                        val code = (resp as SendAuth.Resp).code
//                        if (App.IS_WX_BIND) {
////                           viewModel.bindWeixin(code = code) {
////                               if (it.code == 1000) {
////                                   //startActivity<WeChatBindActivity>("id" to it.data.id)
////                               } else {
////                                   toast(it.msg)
////                               }
////                               finish()
////                           }
//                        } else {
//                            /*viewModel.getWechatUserInfo(code){
//                                postEvent(EventMessage.EventState.TEST_COMMENT_VISIBLE, it)
//                            }*/
//
//                        }
                        Log.i("code", code)
//                            App.isWxLogin = true
                        Constant.isWxLogin = true
                        LiveEventBus.get(LiveEventBusCode.TEST_COMMENT_VISIBLE).post(code)
                        finish()
                    }

                    RETURN_MSG_TYPE_SHARE -> {
                        ToastUtils.showToast("分享成功")
                        finish()
                    }
                }
            }

            else -> {
                LiveEventBus.get(LiveEventBusCode.WECHAT_LOGIN_ERR).post("")
            }
        }
    }

}